﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using JinianNet.JNTemplate.Process.Element;

namespace JinianNet.JNTemplate.Process
{
    public class Parsing
    {
        //readonly static char[] Separator = { '<', '>', '?', ',', '.', '/', ':', '\"', '|', '\\', '\'', ';', '[', ']', '{', '}', '=', '-', ')', '(', '*', '&', '^', '%', '$', '#', '@', '!', '~', '`', '_', '+' };

        #region
        public static bool CompareObject(object x, object y, string operators)
        {
            if (x == null)
                return y == null;
            if (y == null)
                return x == null;
            string str1 = x.ToString(), str2 = y.ToString();
            if (ParserRegex.Number.Match(str1).Success && ParserRegex.Number.Match(str2).Success)
                return CompareExpression(int.Parse(str1), int.Parse(str2), operators);
            if (ParserRegex.Double.Match(str1).Success && ParserRegex.Double.Match(str2).Success)
                return CompareExpression(float.Parse(str1), float.Parse(str2), operators);
            return CompareExpression(str1, str2, operators);
        }

        public static bool CompareExpression(float x, float y, string operators)
        {
            switch (operators)
            {
                case ">":
                    return x > y;
                case "<":
                    return x < y;
                case ">=":
                    return x >= y;
                case "<=":
                    return x <= y;
                case "==":
                    return x == y;
                case "!=":
                    return x != y;
                default:
                    return false;
            }
        }

        public static bool CompareExpression(int x, int y, string operators)
        {
            switch (operators)
            {
                case ">":
                    return x > y;
                case "<":
                    return x < y;
                case ">=":
                    return x >= y;
                case "<=":
                    return x <= y;
                case "==":
                    return x == y;
                case "!=":
                    return x != y;
                default:
                    return false;
            }
        }

        public static bool CompareExpression(string x, string y, string operators)
        {
            if (x == null)
                return y == null;
            if (y == null)
                return x == null;
            switch (operators)
            {
                case "==":
                    return x == y || x.Equals(y);
                case "!=":
                    return x != y || !x.Equals(y);
                default:
                    return false;
            }
        }

        #endregion

        #region
        public static string Calculate(string x, string y, string op)
        {
            if (ParserRegex.Number.Match(x).Success && ParserRegex.Number.Match(y).Success)
                return Convert.ToString(CompareExpression(int.Parse(x), int.Parse(y), op));
            if (ParserRegex.Double.Match(x).Success && ParserRegex.Double.Match(y).Success)
                return Convert.ToString(CompareExpression(float.Parse(x), float.Parse(y), op));
            if (op == "+")
                return string.Concat(x, y);
            else
                return "类型错误";
        }
        public static float Calculate(float x, float y, string op)
        {
            switch (op)
            {
                case "*":
                    return x * y;
                case "/":
                    return x / y;
                case "+":
                    return x + y;
                case "-":
                    return x - y;
                case "%":
                    return x % y;
                default:
                    throw new Exception("operator error");
            }
        }
        public static float Calculate(int x, int y, string op)
        {
            switch (op)
            {
                case "*":
                    return x * y;
                case "/":
                    return x / y;
                case "+":
                    return x + y;
                case "-":
                    return x - y;
                case "%":
                    return x % y;
                default:
                    throw new Exception("operator error");
            }
        }
        #endregion
    }
}
